/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package net.octopeer.message;

import java.io.File;
import java.io.StringReader;

import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;

import org.xml.sax.InputSource;

import net.octopeer.utilities.XMLTools;
import net.octopeer.utilities.SimpleSHA1;

/**
 *
 * @author andria
 */
public class Message {
    private String msgID;
    private String msgType;
    private String msgDetail;

    public Message() {
        //
    }
    
    public Message(String msgType, String msgDetail) throws Exception {
        this.msgType = msgType;
        this.msgDetail = msgDetail;
        this.msgID = SimpleSHA1.SHA1(msgType + msgDetail);
    }
    
    public void setMsgID(String msgID) {
        this.msgID = msgID;
    }
    
    public void setMsgType(String msgType) {
        this.msgType = msgType;
    }
    
    public void setMsgDetail(String msgDetail) {
        this.msgDetail = msgDetail;
    }
    
    public String getMsgID() {
        return this.msgID;
    }
    
    public String getMsgType() {
        return this.msgType;
    }
    
    public String getMsgDetail() {
        return this.msgDetail;
    }
    
    public Document toXMLDoc() throws Exception {
        Document doc;
        XMLTools xt = new XMLTools();        
        doc = xt.db.newDocument();
        
        Element rootEle = doc.createElement("Msg");

        // ID
        Element msgIDEle = doc.createElement("Msg:ID");
        Text msgIDText = doc.createTextNode(this.msgID);
        msgIDEle.appendChild(msgIDText);
        rootEle.appendChild(msgIDEle);        
        
        // Type
        Element msgTypeEle = doc.createElement("Msg:Type");
        Text msgTypeText = doc.createTextNode(this.msgType);
        msgTypeEle.appendChild(msgTypeText);
        rootEle.appendChild(msgTypeEle);        
        
        // Detail
        Element msgDetailEle = doc.createElement("Msg:Detail");
        Text msgDetailText = doc.createTextNode(this.msgDetail);
        msgDetailEle.appendChild(msgDetailText);
        rootEle.appendChild(msgDetailEle);        
        
        doc.appendChild(rootEle);                
        
        return doc;
    }
    
    public String toXMLString() throws Exception {
        String result = null;
        
        XMLTools xt = new XMLTools();                
        result = xt.writeXMLString(this.toXMLDoc());
        
        return result;
    }
    
    public void toXMLFile(String fileName) throws Exception {
        // Prepare the DOM document for writing
        Source source = new DOMSource(this.toXMLDoc());

        // Prepare the output file
        File file = new File(fileName);
        Result result = new StreamResult(file);

        // Write the DOM document to the file
        Transformer xformer = TransformerFactory.newInstance().newTransformer();
        xformer.transform(source, result);
    }
    
    public Message fromXMLDoc(Document dom) {
        Message result = new Message();
        
        Element rootEle = dom.getDocumentElement();
        
        if (rootEle.getTagName().equals("Msg")) {
            NodeList msgIDList = rootEle.getElementsByTagName("Msg:ID");
            String msgIDText = msgIDList.item(0).getFirstChild().getNodeValue();
            NodeList msgTypeList = rootEle.getElementsByTagName("Msg:Type");
            String msgTypeText = msgTypeList.item(0).getFirstChild().getNodeValue();
            NodeList msgDetailList = rootEle.getElementsByTagName("Msg:Detail");
            String msgDetailText = msgDetailList.item(0).getFirstChild().getNodeValue();
            result.setMsgID(msgIDText);
            result.setMsgType(msgTypeText);
            result.setMsgDetail(msgDetailText);
    
        }
                
        return result;
    }
    
    public Message fromXMLString(String data) throws Exception {
        Document dom = null;
        XMLTools xt = new XMLTools();
        Message result = null;
        
        // Parse the XML stream input to Document type
        dom = xt.db.parse(new InputSource(new StringReader(data)));
        
        result = fromXMLDoc(dom);
        
        return result;
    }
    
    public Message fromXMLFile(String fileName) throws Exception {
        Document dom = null;
        File file = null;
        Message result = null;
        XMLTools xt = new XMLTools();
        
        // Parse the XML stream input to Document type
        file = new File(fileName);
        dom = xt.db.parse(file);
        
        result = fromXMLDoc(dom);
        
        return result;
    }
}
